package cn.com.api.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ibm.icu.util.Calendar;







import cn.com.api.entity.ApiU_discover;
import cn.com.api.entity.ApiU_discover_cate;
import cn.com.api.entity.ApiU_discover_resources;
import cn.com.api.entity.ApiU_discover_spec;
import cn.com.api.entity.ApiU_user_label;
import cn.com.api.service.ApiT_memberService;
import cn.com.api.service.ApiT_member_entrepreneurService;
import cn.com.api.service.ApiT_orderService;
import cn.com.api.service.ApiU_discoverService;
import cn.com.api.service.ApiU_discover_cateService;
import cn.com.api.service.ApiU_discover_resourcesService;
import cn.com.api.service.ApiU_discover_specService;
import cn.com.api.service.ApiU_user_labelService;
import cn.com.api.service.CheckService;
import cn.com.util.*;

/**
 * 新发现
 * 
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@Controller
@RequestMapping(Utils.SYS_API + "discover/")
public class ApiU_discoverController {

	@Autowired
	private ApiU_discoverService apiU_discoverService;
	
	@Autowired
	private ApiT_orderService apiT_orderService;
	
	@Autowired
	private CheckService checkService;
	
	@Autowired
	private ApiU_discover_specService apiU_discover_specService;
	
	@Autowired
	private ApiU_discover_resourcesService apiU_discover_resourcesService;
	
	@Autowired
	private ApiU_user_labelService apiUserLabelService;

	@Autowired
	private ApiT_member_entrepreneurService apiT_member_entrepreneurServiceService;
	
	@Autowired
	private ApiT_memberService apiT_memberService;
	
	@Autowired
	private ApiU_discover_cateService apiU_discover_cateService;
	
	private static Logger logger = Logger.getLogger(ApiU_discoverController.class);

	/**
	 * 新增或修改
	 * 
	 * @param spec
	 *            费用规格 费用名:价格:数量*费用名:价格:数量
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "insertOrUpdateDiscover")
	public void insertU_discover(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			ApiU_discover apiU_discover, String spec) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				apiU_discover.setIs_verify(0);
				apiU_discover.setAdd_time(DateUtil.nowTimeToString());
				apiU_discoverService.U_discoverInsertion(apiU_discover);
				if (spec != null && !"".equals(spec)) {
					String[] specs = spec.split("*");
					Integer sort = 1;
					for (String string : specs) {
						ApiU_discover_spec apiU_discover_spec = new ApiU_discover_spec();
						String[] values = string.split(":");
						apiU_discover_spec.setConn_id(apiU_discover.getId() + "");
						apiU_discover_spec.setPrice_name(values[0]);
						apiU_discover_spec.setPrice(new BigDecimal(values[1]));
						apiU_discover_spec.setLimit(Integer.valueOf(values[2]));
						apiU_discover_spec.setType(apiU_discover.getType());
						apiU_discover_spec.setSort(sort);
						apiU_discover_specService.U_discover_specInsertion(apiU_discover_spec);
						sort++;
					}
				}
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 获得我发布的新发现活动列表
	 * 
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getMyDiscover")
	public void getMyU_discovers(HttpServletRequest request, HttpServletResponse response, String user_id, String token, PageInfo pageInfo)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			Map<String, Object> params = new HashMap<String, Object>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
/*				map.clear();
				List<ApiU_discover> u_discovers = this.apiU_discoverService.getU_discoverByUser(user_id);
				map.put("datas", u_discovers);*/
//				String type = request.getParameter("type");
				String cate_type = request.getParameter("cate_type");
				if(cate_type.indexOf(",") != -1){
					params.put("cate_type_all", 1);
					String[] cate_type_list = cate_type.split(",");
					params.put("cate_type_result", cate_type_list);
					
				}else{
					params.put("cate_type_all", 0);
				}
//				params.put("type", type);
				params.put("user_id", user_id);
				params.put("my_user_id", user_id);
				params.put("cate_type", cate_type);
				List<ApiU_discover> u_discovers = this.apiU_discoverService.find_discover_list(params,  pageInfo);				
				map.put("datas", u_discovers);
				
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "查询成功！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 获得我参与的新发现活动列表
	 * 
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getMyJoinDiscover")
	public void getMyJionU_discovers(HttpServletRequest request, HttpServletResponse response, String user_id, String token)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			//String type = request.getParameter("type");
			String cate_type = request.getParameter("cate_type");
			paramMap.put("user_id", user_id);
			//paramMap.put("type", type);
			paramMap.put("token", token);
			paramMap.put("cate_type", cate_type);
			if(cate_type.indexOf(",") != -1){
				paramMap.put("cate_type_all", 1);
				String[] cate_type_list = cate_type.split(",");
				paramMap.put("cate_type_result", cate_type_list);
				
			}else{
				paramMap.put("cate_type_all", 0);
			}
			List<ApiU_discover> u_discovers = this.apiU_discoverService.getU_discoverByJoinUser(paramMap);
			map.put("datas", u_discovers);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
			
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	
	
	@RequestMapping(value = "getApiU_discover_by_loc")
	public void getApiU_discoverByLocation(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> maps = new HashMap<String, Object>();
		String Longitude = request.getParameter("longitude");// 精度
		String Latitude = request.getParameter("latitude");// 维度
		String deviceId = request.getParameter("deviceId");// device id
		String deviceType = request.getParameter("deviceType");// deviceType
		Map<String, Object> params = new HashMap<String, Object>();
		Map<String, String> labelMap = new HashMap<String, String>();
		String cate_type = request.getParameter("cate_type");
		String keywords = request.getParameter("keywords");
		String activity_type = request.getParameter("activity_type");

		params.put("cate_type", cate_type);
		params.put("user_id", user_id);
		params.put("keywords", keywords);
		params.put("activity_type", activity_type);
		
		params.put("longitude", Longitude);
		params.put("latitude", Latitude);
		
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			// 判断当前用户身份是否合法
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {

				List<Map<String, Object>> u_discovers = this.apiU_discoverService.find_discover_by_loc(params);
				for (Map apiU_discover : u_discovers) {
					maps = new HashMap<String, Object>();
					maps.put("id", apiU_discover.get("ID")+"");
					maps.put("average_price", apiU_discover.get("AVERAGE_PRICE"));
					maps.put("info_level", "");
					maps.put("to_user_distance",apiU_discover.get("TO_USER_DISTANCE"));
					maps.put("c_nick", apiU_discover.get("C_NICK"));
					maps.put("c_avatar", apiU_discover.get("C_AVATAR"));
					maps.put("title", apiU_discover.get("TITLE"));
					maps.put("latitude", apiU_discover.get("LATITUDE"));
					maps.put("longitude", apiU_discover.get("LONGITUDE"));
					maps.put("c_desc", "");
					maps.put("description", apiU_discover.get("DESCRIPTION"));
					list.add(maps);					
					
				}
				map = new HashMap<String, Object>();
				params.put("cate_type", "4");
				int publish_count = this.apiU_discoverService.find_discover_publish_count(params);			
				params.put("cate_type", "5");
				publish_count += this.apiU_discoverService.find_discover_publish_count(params);			
				map.put("need_publish_count", publish_count);		
				map.put("service_publish_count", 0);
				
				labelMap.put("type", "3");
				labelMap.put("user_id", user_id);	
				List<ApiU_discover_cate> service_cates = apiU_discover_cateService.findU_discover_byType(5);
				
				List<ApiU_discover_cate> need_cates = apiU_discover_cateService.findU_discover_byType(4);
				
				Map<String, Object> enterMap = new HashMap<String, Object>();
				
				enterMap.put("user_id", user_id);
				enterMap.put("verify_entrepreneur", 1);
				
				List<Map<String, Object>> enterlist = apiT_member_entrepreneurServiceService.findMember_entrepreneursByMap(enterMap);
				if(enterlist != null && enterlist.size() >= 1){
					map.put("conn_status", 1);			
				}else{
					map.put("conn_status", 0);	
				}
				
				map.put("service_list", service_cates);				
				map.put("need_list", need_cates);
				map.put("datas", list);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "查询成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "用户校验未通过！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	
	/**
	 * 与我相关技能开关接口
	 * @param request
	 * @param response
	 * @param user_id
	 * @param token
	 * @throws Exception
	 */
	@RequestMapping(value = "getApiU_discover_service")
	public void getApiU_discover_service(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token) throws Exception {
		Map<String, Object> labelMap = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		String service_type = request.getParameter("service_type");			
		String cate_type = request.getParameter("cate_type");		
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			// 判断当前用户身份是否合法
		
			labelMap.put("type", "3");
			labelMap.put("user_id", user_id);	
				
			List<ApiU_discover_cate> need_cates = apiU_discover_cateService.findU_discover_byType(Integer.parseInt(cate_type));
				
			if(service_type.equals("1")){
				map.put("need_list", need_cates);
			}else{
				List<ApiU_user_label> user_label_list = this.apiUserLabelService.getUserProfessionArea(labelMap);
					
				List<ApiU_discover_cate> need_list = new ArrayList<ApiU_discover_cate>();
					
				for (ApiU_user_label apiU_user_label : user_label_list) {					
				for (ApiU_discover_cate discover_cate : need_cates) {						
						if(discover_cate.getCate_name().equals(apiU_user_label.getLabel_name())){ 
							need_list.add(discover_cate);
							break;
						}
					}
				}				
				map.put("need_list", need_list);
			}
			
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
			
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 * 搜索活动
	 * @param request
	 * @param response
	 * @param pageInfo
	 * @throws Exception
	 */
	@RequestMapping(value = "search_discover")
	public void searchApiU_discover(HttpServletRequest request, HttpServletResponse response, PageInfo pageInfo, String user_id,
			String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("title", request.getParameter("search_text"));
			//params.put("type", request.getParameter("type"));
			params.put("cate_type", request.getParameter("cate_type"));
			params.put("user_id", user_id);
			
			List<ApiU_discover> u_discovers = this.apiU_discoverService.search_discover_info(params, pageInfo);
			int publish_count = this.apiU_discoverService.find_discover_publish_count(params);
			int join_count = this.apiU_discoverService.find_discover_join_count(params);
			map.put("publish_count", publish_count);
			map.put("join_count", join_count);
			map.put("datas", u_discovers);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
			

		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 * 发布活动
	 * @param request
	 * @param response
	 * @param user_id
	 * @param token
	 * @throws Exception
	 */
	@RequestMapping(value = "publish_discover")
	public void publishApiU_discover(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				Integer type = Integer.valueOf(request.getParameter("type"));
//				if(!apiT_memberService.findT_membersVerify(type, user_id)){
//					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
//					map.put(Utils.MESSAGE, "无权限发布");
//				}else{
				
					// 下列的传入参数全部都以post方式传上来，并且需要进行url encoder转码，服务端进行解码
					String cateId = request.getParameter("cateId");
					String headImg = request.getParameter("head_img");
					String vedio = request.getParameter("vedio");
					String title = request.getParameter("title");
					String address = request.getParameter("address");
					
					String longitude = request.getParameter("longitude");
					String latitude = request.getParameter("latitude");
					String people_limit = request.getParameter("people_limit");
					String beginTime = request.getParameter("begin_time");
					String endTime = request.getParameter("end_time");
					String description = request.getParameter("description");
					String average_price = request.getParameter("average_price");
					Integer isAdd = Integer.valueOf(request.getParameter("is_add"));// 是否加入供需，如果是活动（type=0，这个值恒定为0）
					String str_chargenames = request.getParameter("chargenames");
					String str_chargefees = request.getParameter("chargefees");
					String str_chargelimits = request.getParameter("chargelimits");
					
					String[] chargenames = null;
					String[] chargefees = null;
					String[] chargelimits = null;
					
					
					if(str_chargenames != null && !str_chargenames.equals("")){
						chargenames = str_chargenames.split("@#@");// 费用明细：多条，每条之间用@#@分隔开
					}
					if(str_chargefees != null && !str_chargefees.equals("")){
						chargefees = str_chargefees.split("@#@");
					}
					if(str_chargelimits != null && !str_chargelimits.equals("")){
						chargelimits = str_chargelimits.split("@#@");
					}
					
	

					
					/**huangshi001 add start**/
					String image_all = request.getParameter("image_all");
					String vedio_all = request.getParameter("vedio_all");
					String tape_all = request.getParameter("tape_all");
					String[] image_str = null;
					String[] vedio_str = null;
					String[] tape_str = null;
					if(image_all != null){
						image_str = image_all.split("@#@");
					}
					if(vedio_all != null){					
						vedio_str = vedio_all.split("@#@");
					}
					if(tape_all != null){					
						tape_str = tape_all.split("@#@");
					}
					/**huangshi001 add end**/
					
					String validateResult = this.validateAddDiscover(type, cateId, vedio, title, address, beginTime,
							endTime, isAdd, chargenames, chargefees, chargelimits);
	
					if (validateResult == null) {
	
						ApiU_discover apiU_discover = new ApiU_discover();
	
						apiU_discover.setActivity_type(cateId);
						apiU_discover.setAddress(address);
						apiU_discover.setVedio(vedio);
						apiU_discover.setTitle(title);
						apiU_discover.setHead_img(headImg);
						apiU_discover.setLatitude(latitude);
						apiU_discover.setBegin_time(beginTime);
						apiU_discover.setEnd_time(endTime);
						apiU_discover.setLongitude(longitude);
						apiU_discover.setDescription(description);
						apiU_discover.setIs_add(isAdd);
						apiU_discover.setPeople_limit(people_limit);
						try {
							
							if(average_price == null || average_price.equals("") ){
								apiU_discover.setAverage_price(BigDecimal.ZERO);
							}else{							
								apiU_discover.setAverage_price(new BigDecimal(average_price));
							}
						} catch (Exception e) {
							apiU_discover.setAverage_price(BigDecimal.ZERO);
						}
						apiU_discover.setIs_verify(0);
						apiU_discover.setIs_delete(0);
						apiU_discover.setIs_cancel(0);
						apiU_discover.setAdd_time(
								new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
						apiU_discover.setUser_id(user_id);
						apiU_discover.setType(type);
	
						List<ApiU_discover_spec> specs = new ArrayList<ApiU_discover_spec>();
						if(chargenames != null && chargenames.length >= 1){
						for (int i = 0; i < chargenames.length; i++) {
							ApiU_discover_spec apiU_discover_spec = new ApiU_discover_spec();
	
							apiU_discover_spec.setPrice_name(chargenames[i]);							
							try {								
								apiU_discover_spec.setPrice(new BigDecimal(chargefees[i]));
							} catch (Exception e) {
								apiU_discover_spec.setPrice(BigDecimal.ZERO);
							}
							

							try {								
								apiU_discover_spec.setLimit(Integer.valueOf(chargelimits[i]));
							} catch (Exception e) {
								apiU_discover_spec.setLimit(0);
							}
					
							
	
							apiU_discover_spec.setSort(i + 1);
							apiU_discover_spec.setType(0);
	
							specs.add(apiU_discover_spec);
						}
						}
						
						/**huangshi001 add start**/
						int j = 0;
						List<ApiU_discover_resources> resources = new ArrayList<ApiU_discover_resources>();
						if(image_str != null && image_str.length >= 1){
							for (int i = 0; i < image_str.length; i++) {
								ApiU_discover_resources apiU_discover_resources =  new ApiU_discover_resources();
								apiU_discover_resources.setResources(image_str[i]);
								apiU_discover_resources.setResources_type(0);
								apiU_discover_resources.setType(0);
								apiU_discover_resources.setSort(j);
								resources.add(apiU_discover_resources);
								j++;
								
							}
						}
						if(vedio_str != null && vedio_str.length >= 1){
							for (int i = 0; i < vedio_str.length; i++) {
								ApiU_discover_resources apiU_discover_resources =  new ApiU_discover_resources();
								apiU_discover_resources.setResources(vedio_str[i]);
								apiU_discover_resources.setResources_type(1);
								apiU_discover_resources.setType(0);
								apiU_discover_resources.setSort(j);
								resources.add(apiU_discover_resources);
								j++;
							}
						}
						if(tape_str != null && tape_str.length >= 1){
							for (int i = 0; i < tape_str.length; i++) {
								ApiU_discover_resources apiU_discover_resources =  new ApiU_discover_resources();
								apiU_discover_resources.setResources(tape_str[i]);
								apiU_discover_resources.setResources_type(2);
								apiU_discover_resources.setType(0);
								apiU_discover_resources.setSort(j);
								resources.add(apiU_discover_resources);
								j++;
							}
						}
						/**huangshi001 add end**/
	
						this.apiU_discoverService.U_discoverInsertionWithSpec(apiU_discover, specs, resources);
	
						map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
						map.put(Utils.MESSAGE, "发布成功！");
					} else {
						map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
						map.put(Utils.MESSAGE, validateResult);
					}
				}
		//	}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	private String validateAddDiscover(Integer type, String cateId, String vedio, String title, String address,
			String beginTime, String endTime, Integer isAdd, String[] chargenames, String[] chargefees,
			String[] chargelimits) {

		if (type == 1 || type == 0) {
			if (cateId.equalsIgnoreCase("")) {
				return "分类不可为空";
			}

			if (vedio.equalsIgnoreCase("")) {
				return "音乐不可为空";
			}
		}

		if (type == 0 && isAdd != 0) {
			return "发布新发现活动时无需指定是否加入供需";
		}

		if (title.equalsIgnoreCase("")) {
			return "名称/标题/关键字不可为空";
		}

		if (address.equalsIgnoreCase("")) {
			return "详细地址不可为空";
		}
		
		if(beginTime != null && !("").equals(beginTime)){
			try {				
				SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date s1 = sd.parse(beginTime);
			} catch (Exception e) {
				return "日期格式不符合";
			}
		}
		if(endTime != null && !("").equals(endTime)){
			try {				
				SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date s1 = sd.parse(endTime);
			} catch (Exception e) {
				return "日期格式不符合";
			}
		}

		return null;
	}

	/**
	 * 与我技能相关的需求
	 * 
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getDiscoverUserAbility")
	public void getDiscoverUserAbility(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String label_info = request.getParameter("keywords");
		try {
			Map<String, String> params = new HashMap<String, String>();
			params.put("user_id", user_id);
			params.put("type", "3");
			params.put("label_info", label_info);
			if (checkService.checkToken(user_id, token, map)) {
				List<ApiU_user_label> user_label_list = this.apiUserLabelService.getUserProfessionArea(params);
				String[] labels = new String[user_label_list.size()];
				for (int i = 0; i < labels.length; i++) {
					labels[i] = user_label_list.get(i).getLabel_name();
				}
				List<ApiU_discover> u_discovers = this.apiU_discoverService.getU_discoverByUserAbility(labels, 2);
				map.put("datas", u_discovers);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成");
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 找专业的服务
	 * 
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "findProfessionalService")
	public void findProfessionalService(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> params = new HashMap<String, String>();
			params.put("user_id", user_id);
			params.put("type", "3");

			if (checkService.checkToken(user_id, token, map)) {
				String keywords = request.getParameter("keywords");
				String[] keyLabels = keywords.split(",");
				List<ApiU_discover> u_discovers = this.apiU_discoverService.getU_discoverByUserAbility(keyLabels, 1);
				map.put("datas", u_discovers);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成");
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 * 获取展会列表信息
	 * @param request
	 * @param response
	 * @param user_id
	 * @param token
	 * @throws Exception
	 */
	@RequestMapping(value = "find_discover-exhibition")
	public void find_discover_exhibition(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, PageInfo pageInfo) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("user_id", user_id);


			if (checkService.checkToken(user_id, token, map)) {
				String cate_name = request.getParameter("cate_name");
				//String type = request.getParameter("type");
				params.put("cate_name", cate_name);
				//params.put("type", type);
				List<ApiU_discover> u_discovers = this.apiU_discoverService.findU_discovers_exhibitionByMap(params,  pageInfo);
				int publish_count = this.apiU_discoverService.find_discover_publish_count(params);
				int join_count = this.apiU_discoverService.find_discover_join_count(params);
				map.put("publish_count", publish_count);
				map.put("join_count", join_count);
				map.put("datas", u_discovers);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成");
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	
	/**
	 * 获取discover列表信息
	 * @param request
	 * @param response
	 * @param user_id
	 * @param token
	 * @throws Exception
	 */
	@RequestMapping(value = "find_discover-list")
	public void find_discover_list(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, PageInfo pageInfo) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			//if (checkService.checkToken(user_id, token, map)) {
				//String type = request.getParameter("type");
				String cate_type = request.getParameter("cate_type");
				//params.put("type", type);
				params.put("cate_type", cate_type);
				params.put("user_id", user_id);
				if("1".equals(cate_type)){					
					String activity_type = request.getParameter("s_hangye");
					String area = request.getParameter("s_area");
					String s_price = request.getParameter("s_price");
					params.put("activity_type", activity_type);
					params.put("area", area);
					params.put("price", s_price);
				}
				
				List<ApiU_discover> u_discovers = this.apiU_discoverService.find_discover_list(params,  pageInfo);
				BigDecimal low_price = BigDecimal.ZERO;
				for (ApiU_discover apiU_discover : u_discovers) {
					List<ApiU_discover_spec> spec_list = this.apiU_discover_specService.getApiU_discover_specsBydiscover_Id(apiU_discover.getId()+"");
					int i = 0;
					for (ApiU_discover_spec apiU_discover_spec : spec_list) {						

						if(low_price.compareTo(apiU_discover_spec.getPrice()) >= 0 || i == 0){							
							low_price = apiU_discover_spec.getPrice();
							apiU_discover.setShow_price(low_price);
						}
						i++;
					}
				}
				
				int publish_count = this.apiU_discoverService.find_discover_publish_count(params);
				int join_count = this.apiU_discoverService.find_discover_join_count(params);
				map.put("publish_count", publish_count);
				map.put("join_count", join_count);
				map.put("datas", u_discovers);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成");
		//	}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	
	/**
	 * 获取活动详情
	 * @param request
	 * @param response
	 * @param user_id
	 * @param token
	 * @throws Exception
	 */
	@RequestMapping(value = "find_discover-detail")
	public void find_discover_detail(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token) throws Exception {
		Map<String, Object> resMap = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> params = new HashMap<String, String>();
			params.put("user_id", user_id);

			//if (checkService.checkToken(user_id, token, map)) {
				String discover_id = request.getParameter("discover_id");
				params.put("discover_id", discover_id);
				ApiU_discover u_discover = this.apiU_discoverService.findU_discovers_detail(params);
				int is_enroll = this.apiU_discoverService.findU_discovers_status_byUserid(params);//参加活动次数
				if(is_enroll >= 1){
					u_discover.setIs_enroll(1);
				}
				else{
					u_discover.setIs_enroll(0);
				}
				
				Map<String, Object> member_info = this.apiT_member_entrepreneurServiceService.getMemberByUserId(u_discover.getUser_id(), user_id);//参数为发布者USERID 
				List<ApiU_discover_spec> spec_list = this.apiU_discover_specService.getApiU_discover_specsBydiscover_Id(discover_id);
				List<ApiU_discover_resources> resource_list = this.apiU_discover_resourcesService.getApiU_discover_resourcesBydiscover_Id(discover_id);

				List<ApiU_discover_spec> spec_res_list = new ArrayList<ApiU_discover_spec>();
				for (ApiU_discover_spec apiU_discover_spec : spec_list) {
					int limit = 0;
					if(apiU_discover_spec.getLimit() != null && !apiU_discover_spec.getLimit().equals("")){
						limit = apiU_discover_spec.getLimit();
					}
					apiU_discover_spec.setSpec_res_count(limit - apiU_discover_spec.getSpec_count() );
					spec_res_list.add(apiU_discover_spec);
					
				}
				resMap.put("discover_info", u_discover);
				resMap.put("member_info", member_info); 
				resMap.put("spec_info", spec_res_list);
				resMap.put("resource_info", resource_list);
				map.put("datas", resMap);
				
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成");
	//		}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	
	
	/**
	 * 转发活动增加次数
	 * @param request
	 * @param response
	 * @param user_id
	 * @param token
	 * @throws Exception
	 */
	@RequestMapping(value = "addShareDiscover")
	public void insertU_discoverShare(HttpServletRequest request, HttpServletResponse response, String user_id, String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			String discover_id = request.getParameter("discover_id");
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				
				paramMap.put("discover_id", discover_id);
				this.apiU_discoverService.insertU_discoverShare(paramMap);
				
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "分享成功！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	
	@RequestMapping(value = "discover-delete")
	public void u_discover_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("discover_id");

		// delete
		int ret_i = apiU_discoverService.deleteU_discover(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 * 修改发布活动
	 * @param request
	 * @param response
	 * @param user_id
	 * @param token
	 * @throws Exception
	 */
	@RequestMapping(value = "update_discover")
	public void update_discover(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				Integer type = Integer.valueOf(request.getParameter("type"));
				
					// 下列的传入参数全部都以post方式传上来，并且需要进行url encoder转码，服务端进行解码
					String cateId = request.getParameter("cateId");
					String headImg = request.getParameter("head_img");
					String vedio = request.getParameter("vedio");
					String title = request.getParameter("title");
					String address = request.getParameter("address");
					
					String longitude = request.getParameter("longitude");
					String latitude = request.getParameter("latitude");
					
					String beginTime = request.getParameter("begin_time");
					String endTime = request.getParameter("end_time");
					String description = request.getParameter("description");
					String average_price = request.getParameter("average_price");
					Integer isAdd = Integer.valueOf(request.getParameter("is_add"));// 是否加入供需，如果是活动（type=0，这个值恒定为0）
					String str_chargenames = request.getParameter("chargenames");
					String str_chargefees = request.getParameter("chargefees");
					String str_chargelimits = request.getParameter("chargelimits");
					int discoverid = Integer.parseInt(request.getParameter("discover_id"));
					
					String[] chargenames = null;
					String[] chargefees = null;
					String[] chargelimits = null;
					
					
					if(str_chargenames != null && !str_chargenames.equals("")){
						chargenames = str_chargenames.split("@#@");// 费用明细：多条，每条之间用@#@分隔开
					}
					if(str_chargefees != null && !str_chargefees.equals("")){
						chargefees = str_chargefees.split("@#@");
					}
					if(str_chargelimits != null && !str_chargelimits.equals("")){
						chargelimits = str_chargelimits.split("@#@");
					}
					
	

					
					/**huangshi001 add start**/
					String image_all = request.getParameter("image_all");
					String vedio_all = request.getParameter("vedio_all");
					String tape_all = request.getParameter("tape_all");
					String[] image_str = null;
					String[] vedio_str = null;
					String[] tape_str = null;
					if(image_all != null){
						image_str = image_all.split("@#@");
					}
					if(vedio_all != null){					
						vedio_str = vedio_all.split("@#@");
					}
					if(tape_all != null){					
						tape_str = tape_all.split("@#@");
					}
					/**huangshi001 add end**/
					
					String validateResult = this.validateAddDiscover(type, cateId, vedio, title, address, beginTime,
							endTime, isAdd, chargenames, chargefees, chargelimits);
	
					if (validateResult == null) {
	
						ApiU_discover apiU_discover = new ApiU_discover();
						
						
						apiU_discover.setActivity_type(cateId);
						apiU_discover.setAddress(address);
						apiU_discover.setVedio(vedio);
						apiU_discover.setTitle(title);
						apiU_discover.setHead_img(headImg);
						apiU_discover.setLatitude(latitude);
						apiU_discover.setBegin_time(beginTime);
						apiU_discover.setEnd_time(endTime);
						apiU_discover.setLongitude(longitude);
						apiU_discover.setDescription(description);
						apiU_discover.setIs_add(isAdd);
						apiU_discover.setId(discoverid);
						
						try {
							
							if(average_price == null || average_price.equals("") ){
								apiU_discover.setAverage_price(BigDecimal.ZERO);
							}else{							
								apiU_discover.setAverage_price(new BigDecimal(average_price));
							}
						} catch (Exception e) {
							apiU_discover.setAverage_price(BigDecimal.ZERO);
						}
						apiU_discover.setIs_verify(0);
						apiU_discover.setIs_delete(0);
						apiU_discover.setIs_cancel(0);
						apiU_discover.setAdd_time(
								new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
						apiU_discover.setUser_id(user_id);
						apiU_discover.setType(type);
	
						List<ApiU_discover_spec> specs = new ArrayList<ApiU_discover_spec>();
						if(chargenames != null && chargenames.length >= 1){
						for (int i = 0; i < chargenames.length; i++) {
							ApiU_discover_spec apiU_discover_spec = new ApiU_discover_spec();
	
							apiU_discover_spec.setPrice_name(chargenames[i]);							
							try {								
								apiU_discover_spec.setPrice(new BigDecimal(chargefees[i]));
							} catch (Exception e) {
								apiU_discover_spec.setPrice(BigDecimal.ZERO);
							}
							

							try {								
								apiU_discover_spec.setLimit(Integer.valueOf(chargelimits[i]));
							} catch (Exception e) {
								apiU_discover_spec.setLimit(0);
							}
					
							apiU_discover.setPeople_limit(chargelimits[i]);
	
							apiU_discover_spec.setSort(i + 1);
							apiU_discover_spec.setType(0);
	
							specs.add(apiU_discover_spec);
						}
						}
						
						/**huangshi001 add start**/
						int j = 0;
						List<ApiU_discover_resources> resources = new ArrayList<ApiU_discover_resources>();
						if(image_str != null && image_str.length >= 1){
							for (int i = 0; i < image_str.length; i++) {
								ApiU_discover_resources apiU_discover_resources =  new ApiU_discover_resources();
								apiU_discover_resources.setResources(image_str[i]);
								apiU_discover_resources.setResources_type(0);
								apiU_discover_resources.setType(0);
								apiU_discover_resources.setSort(j);
								resources.add(apiU_discover_resources);
								j++;
								
							}
						}
						if(vedio_str != null && vedio_str.length >= 1){
							for (int i = 0; i < vedio_str.length; i++) {
								ApiU_discover_resources apiU_discover_resources =  new ApiU_discover_resources();
								apiU_discover_resources.setResources(vedio_str[i]);
								apiU_discover_resources.setResources_type(1);
								apiU_discover_resources.setType(0);
								apiU_discover_resources.setSort(j);
								resources.add(apiU_discover_resources);
								j++;
							}
						}
						if(tape_str != null && tape_str.length >= 1){
							for (int i = 0; i < tape_str.length; i++) {
								ApiU_discover_resources apiU_discover_resources =  new ApiU_discover_resources();
								apiU_discover_resources.setResources(tape_str[i]);
								apiU_discover_resources.setResources_type(2);
								apiU_discover_resources.setType(0);
								apiU_discover_resources.setSort(j);
								resources.add(apiU_discover_resources);
								j++;
							}
						}
						/**huangshi001 add end**/
	
						this.apiU_discoverService.U_discoverupdateWithSpec(apiU_discover, specs, resources);
	
						map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
						map.put(Utils.MESSAGE, "修改成功！");
					} else {
						map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
						map.put(Utils.MESSAGE, validateResult);
					}
				}
		//	}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
}
